<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends MY_Controller {

	public $_data = array();

	public function __construct() {
		parent::__construct();
		$this->load->model('auth_model', 'auth');
	}

	/**
	 * 菜单管理
	 * @return [type] [description]
	 */
	public function index() {
		$nodes = $this->auth->get_nodes();
		$rules = node_merge($nodes);
		$this->_data['nodes'] = $rules;
		$this->load->view('admin/auth/index.html', $this->_data);
	}

	public function roles()  {
		$this->_data['roles'] = $this->auth->get_roles();
		$this->load->view('admin/auth/roles.html', $this->_data);
	}

	public function users() {
		//调取所有用户
		$this->load->model('admin_model', 'admin');
		$users = $this->admin->get_admin();
		$this->_data['users'] = $users;
		// print_r($users);
		// exit;
		$this->load->view('admin/auth/users.html', $this->_data);
	}

	/**
	 * 添加和编辑用户
	 * @return [type] [description]
	 */
	public function post_user() {
		$this->load->model('admin_model', 'admin');
		$this->load->library('form_validation');

		$uid = $this->uri->segment(4);
		$data = $this->input->post();

		$roles = $this->auth->get_roles();
		
		if (false == $uid) {
			$this->_add_user($data);
		} else {
			$this->_edit_user($uid, $data);
		}

		$this->_data['roles'] = $roles;
		// print_r($this->_data);
		// exit;
		$this->load->view('admin/auth/user_form.html', $this->_data);
	}

	private function _add_user($data) {
		if ($data) {
			$passwd = md5($data['password']);
			$repasswd = md5($data['repassword']);
			if ($passwd != $repasswd) {
				$this->session->set_tempdata('error', '两次输入密码不一致', 5);
				redirect('admin/auth/post_user');
			}
			unset($data['repassword']);
			$data['password'] = $passwd;
			$uid = $this->admin->add_user($data);
			if ($uid) {
				redirect('admin/auth/users');
			}
		}

	}

	private function _edit_user($uid, $data) {
		if ($data) {
			$passwd = md5($data['password']);
			$repasswd = md5($data['repassword']);
			if ($passwd != $repasswd) {
				$this->session->set_tempdata('error', '两次输入密码不一致', 5);
				redirect('admin/auth/post_user/'.$uid);
			}
			unset($data['repassword']);
			$data['password'] = $passwd;
			$this->admin->edit_user($uid, $data);
		} else {
			$user = $this->admin->get_admin($uid);
			$this->_data['user'] = $user;
		}
	}

	public function post_role() {
		$role_id = $this->uri->segment(4);
		if (FALSE == $role_id) {
			$data = $this->input->post();

			$result = $this->auth->add_role($data);
			redirect('admin/auth/roles');
		} else {
			if (empty($_POST)) {
				$data = $this->auth->get_role($role_id);
				$this->_data['role'] = $data;
				$this->load->view('admin/platform/form.html', $this->_data);

			} else {
				$data = $this->input->post();
				$this->auth->edit_role($role_id, $data);
				redirect('admin/auth/roles');
			}
		}
	}

	public function edit_role() {
		$title = $this->input->post('value');
		$gid = $this->input->post('row_id');
		$data = array(
			'title' => $title,
			);
		$status = $this->auth->edit_role($gid, $data);
		if ($status) {
			echo $title;
			exit;
		}

	}

	/**
	 * 添加菜单
	 */
	public function add_node() {
		$this->load->library('form_validation');
		$pid = $this->uri->segment(4);
		$this->_add_node($pid);
	}

	public function node() {
		$this->load->library('form_validation');
		$rid = $this->uri->segment(4);
		if (empty($rid)) {
			$this->_add_node();
		} else {
			$this->_edit_node($rid);
		}
	}


	private function _add_node($pid = 0) {
		if (empty($_POST)) {
			$this->_data['pid'] = $pid;
			$this->load->view('admin/auth/node_form.html', $this->_data);
		} else {
			if (empty($pid)) {
				redirect('admin/auth');
			}
			$data = $this->input->post();
			$data['pid'] = $pid;

			$query_status = $this->auth->add_node($data);
			if ($query_status) {
				redirect('admin/auth');
			}
		}
		
	}

	private function _edit_node($nid) {
		if (empty($_POST)) {
			$node = $this->auth->get_node($nid);
			$this->_data['pid'] = $node['pid'];
			$this->_data['node'] = $node;
			$this->load->view('admin/auth/node_form.html', $this->_data);
		} else {
			$data = $this->input->post();
			$status = $this->auth->edit_node($nid, $data);
			if (!$status) {
				return false;
			}
			redirect('admin/auth');
		}
	}

	/**
	 * 设置权限
	 */
	public function access() {
		$this->load->driver('cache');
		$gid = $this->uri->segment(4);
		if ($_POST) {
			$checked_inputs = $this->input->post('rules');
			$rules = implode(',', $checked_inputs);
			$data = array(
				'rules' => $rules
				);
			$status = $this->auth->set_access($gid, $data);
			//更新缓存
			$cache_key = 'admin_gid_'.$gid;
			$nodes = $this->auth->get_nodes();
			$rules = node_merge($nodes, $checked_inputs);
			$this->cache->file->save($cache_key, $rules, 86400 * 30);
			if ($status) {
				redirect('admin/auth/roles');
			}
		} else {
			//读取所有权限
			$nodes = $this->auth->get_nodes();
			$access = '';

			$cache_key = 'admin_gid_'.$gid;
			$rules = $this->cache->file->get($cache_key);
			if  (empty($rules)) {
				//读取角色的权限
				$g_access = $this->auth->get_access($gid);
				if ($g_access) {
					$access = explode(',', $g_access['rules']);
				}
				$rules = node_merge($nodes, $access);
				$this->cache->file->save($cache_key, $rules, 86400 * 30);
			}

			$this->_data['nodes'] = $rules;
			$this->load->view('admin/auth/access.html', $this->_data);
		}
	}

}